/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.reciever.process;

import dom.Command;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 *
 * @author julian.pena
 */
public class Unzip extends ProcessReciever{

    public Unzip(InputStream inputStream, OutputStream outputStream) {
        super(inputStream, outputStream);
    }

    public Unzip(InputStream inputStream, OutputStream outputStream, Command command) {
        super(inputStream, outputStream, command);
    }
    public Unzip() {
        super(null, null);
    }

    @Override
    public void action() throws Exception {
        while (fileName.equals("")) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException interruptedException) {}
        }
        stop = false;
        byte[] buf = new byte[BUFFER_SIZE];
        while (true) {
            if (stop) {
                stop = false;
                if (fileName == null) {
                    break;
                }
            }
            System.out.println("Descomprimiendo: " + fileName);
            ZipInputStream stream = new ZipInputStream(inputStream);
            ZipEntry entry;
            while((entry = stream.getNextEntry())!=null){
                String name = entry.getName();
                this.notifyStop(name);
                int len;
                while ((stream.available()) > 0) {
                    len = stream.read(buf);
                    if (len == -1) {
                        break;
                    }
                    outputStream.write(buf, 0, len);
                }
            }
        }
        System.out.println("termino de descomprimir");
        super.notifyStop(null);
        outputStream.close();
    }

    @Override
    public boolean isInputReady() {
        if (inputStream != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isOutputReady() {
        if (outputStream != null) {
            return true;
        } else {
            return false;
        }
    }
    
}
